package org.maven.repositorysnapshot;
/**********************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Contributors:

**********************************************************************/
import org.maven.dependencybrowser.DependencyBrowser;
import org.maven.dependencybrowser.gui.ProgressMeter;
import org.maven.dependencybrowser.implementations.Repository;
import org.maven.dependencybrowser.implementations.Workspace;
import org.maven.dependencybrowser.implementations.local.LocalDiskRepository;
import org.maven.dependencybrowser.implementations.local.LocalDiskWorkspace;

import javax.swing.*;
import java.awt.*;
import java.io.File;
import java.io.IOException;
import java.util.zip.ZipException;

public class RepositorySnapshotFactory
{

    private Workspace workspace;
    private Repository repository;
    private String repositoryType;
    private JFrame frame;

    public boolean isSnapshotEnabled()
    {
        return DependencyBrowser.TYPE_LOCAL.equals(repositoryType);
    }

    public void createSnapshot(ProgressMeter progressMeter)
    {
        JFileChooser fc = new JFileChooser()
        {

            public void updateUI()
            {
                // workaround for bug
                // http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6317789
                putClientProperty("FileChooser.useShellFolder", Boolean.FALSE);
                super.updateUI();
            }
        };
        fc.setDialogTitle("Save repository snapshot file");
        int returnVal = fc.showSaveDialog(frame);
        File snapshotFile = fc.getSelectedFile();
        if ( snapshotFile != null )
        {
            if (!snapshotFile.getName().endsWith(".zip"))
            {
                snapshotFile = new File(snapshotFile.getParentFile(), snapshotFile.getName() + ".zip");
            }
            if (returnVal == JFileChooser.APPROVE_OPTION)
            {
                LocalDiskRepository localDiskRepository = (LocalDiskRepository) repository;
                LocalDiskWorkspace localDiskWorkspace = (LocalDiskWorkspace) workspace;
                RepositorySnapshot snapshotCreator = new RepositorySnapshot(localDiskRepository.getRepositoryRoot(), snapshotFile, localDiskWorkspace.getWorkspaceDirectory());
                Thread snapshotCreatorThread = new Thread(new SnapshotCreatorThread(snapshotCreator, progressMeter));
                snapshotCreatorThread.setDaemon(true);
                snapshotCreatorThread.setName("snapshot-creator");
                snapshotCreatorThread.start();
            }
        }
    }

    private class SnapshotCreatorThread implements Runnable
    {

        private RepositorySnapshot snapshotCreator;
        private ProgressMeter progressMeter;

        public SnapshotCreatorThread(RepositorySnapshot snapshotCreator, ProgressMeter progressMeter)
        {
            this.snapshotCreator = snapshotCreator;
            this.progressMeter = progressMeter;
        }

        public void run()
        {
            frame.setCursor(new Cursor(Cursor.WAIT_CURSOR));
            try
            {
                snapshotCreator.createSnapshot(progressMeter);
                progressMeter.snapshotComplete();
            }
            catch (ZipException e)
            {
                progressMeter.error(e);
            }
            catch (IOException e)
            {
                progressMeter.error(e);
            }
            frame.setCursor(new Cursor(Cursor.DEFAULT_CURSOR));
        }
    }

}
